<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>3. 动态请求子节点 | 被删的前端游乐场</title>
    <meta name="generator" content="VuePress 1.8.2">
    
    <meta name="description" content="Just playing around">
    
    <link rel="preload" href="/front-end-playground/assets/css/0.styles.6ad2a9ca.css" as="style"><link rel="preload" href="/front-end-playground/assets/js/app.1e2670bf.js" as="script"><link rel="preload" href="/front-end-playground/assets/js/2.38d016d1.js" as="script"><link rel="preload" href="/front-end-playground/assets/js/3.e3f029cb.js" as="script"><link rel="preload" href="/front-end-playground/assets/js/97.2386ebe1.js" as="script">
    <link rel="stylesheet" href="/front-end-playground/assets/css/0.styles.6ad2a9ca.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/front-end-playground/" class="home-link router-link-active"><!----> <span class="site-name">被删的前端游乐场</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/front-end-playground/" class="nav-link">概述</a></div><div class="nav-item"><a href="/front-end-playground/front-end-basic/" class="nav-link">前端领域</a></div><div class="nav-item"><a href="/front-end-playground/vue/" class="nav-link">Vue学习</a></div><div class="nav-item"><a href="/front-end-playground/wxapp/" class="nav-link">小程序学习</a></div><div class="nav-item"><a href="/front-end-playground/front-end-others/" class="nav-link router-link-active">百家齐放</a></div><div class="nav-item"><a href="/front-end-playground/front-end-addon/" class="nav-link">前端的进击</a></div><div class="nav-item"><a href="/front-end-playground/front-end-work/" class="nav-link">前端与工作</a></div><div class="nav-item"><a href="/front-end-playground/faq.html" class="nav-link">FAQ</a></div> <a href="https://github.com/godbasin/front-end-playground" target="_blank" rel="noopener noreferrer" class="repo-link">
    Github
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/front-end-playground/" class="nav-link">概述</a></div><div class="nav-item"><a href="/front-end-playground/front-end-basic/" class="nav-link">前端领域</a></div><div class="nav-item"><a href="/front-end-playground/vue/" class="nav-link">Vue学习</a></div><div class="nav-item"><a href="/front-end-playground/wxapp/" class="nav-link">小程序学习</a></div><div class="nav-item"><a href="/front-end-playground/front-end-others/" class="nav-link router-link-active">百家齐放</a></div><div class="nav-item"><a href="/front-end-playground/front-end-addon/" class="nav-link">前端的进击</a></div><div class="nav-item"><a href="/front-end-playground/front-end-work/" class="nav-link">前端与工作</a></div><div class="nav-item"><a href="/front-end-playground/faq.html" class="nav-link">FAQ</a></div> <a href="https://github.com/godbasin/front-end-playground" target="_blank" rel="noopener noreferrer" class="repo-link">
    Github
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0" style="padding-top:;"><!----> <p class="sidebar-heading"><span>Angular框架解读</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0" style="padding-top:;"><!----> <p class="sidebar-heading"><span>玩转 Angular</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0" style="padding-top:;"><!----> <p class="sidebar-heading"><span>试试 Cycle.js</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0" style="padding-top:10px;"><div class="kitty-main" data-v-2b653b36><span class="stand" data-v-2b653b36></span> <div class="cat" data-v-2b653b36><div class="body" data-v-2b653b36></div> <div class="head" data-v-2b653b36><div class="ear" data-v-2b653b36></div> <div class="ear" data-v-2b653b36></div></div> <div class="face" data-v-2b653b36><div class="nose" data-v-2b653b36></div> <div class="whisker-container" data-v-2b653b36><div class="whisker" data-v-2b653b36></div> <div class="whisker" data-v-2b653b36></div></div> <div class="whisker-container" data-v-2b653b36><div class="whisker" data-v-2b653b36></div> <div class="whisker" data-v-2b653b36></div></div></div> <div class="tail-container" data-v-2b653b36><div class="tail" data-v-2b653b36><div class="tail" data-v-2b653b36><div class="tail" data-v-2b653b36><div class="tail" data-v-2b653b36><div class="tail" data-v-2b653b36><div class="tail" data-v-2b653b36><div class="tail" data-v-2b653b36></div></div></div></div></div></div></div></div></div></div> <p class="sidebar-heading open"><span>D3.js 和 Tree</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/front-end-playground/front-end-others/d3-tree/1-svg-and-d3.html" class="sidebar-link">1. 拜见 D3.js</a></li><li><a href="/front-end-playground/front-end-others/d3-tree/2-init-a-d3-tree.html" class="sidebar-link">2. 简单的 Tree demo</a></li><li><a href="/front-end-playground/front-end-others/d3-tree/3-ajax-for-node.html" aria-current="page" class="active sidebar-link">3. 动态请求子节点</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/front-end-playground/front-end-others/d3-tree/3-ajax-for-node.html#拆分模块" class="sidebar-link">拆分模块</a></li><li class="sidebar-sub-header"><a href="/front-end-playground/front-end-others/d3-tree/3-ajax-for-node.html#tree-node" class="sidebar-link">Tree node</a></li><li class="sidebar-sub-header"><a href="/front-end-playground/front-end-others/d3-tree/3-ajax-for-node.html#绑定双击事件" class="sidebar-link">绑定双击事件</a></li><li class="sidebar-sub-header"><a href="/front-end-playground/front-end-others/d3-tree/3-ajax-for-node.html#节点状态定义" class="sidebar-link">节点状态定义</a></li><li class="sidebar-sub-header"><a href="/front-end-playground/front-end-others/d3-tree/3-ajax-for-node.html#双击单击的冲突解决" class="sidebar-link">双击单击的冲突解决</a></li></ul></li><li><a href="/front-end-playground/front-end-others/d3-tree/4-zoom-amd-drag.html" class="sidebar-link">4. 添加拖动和缩放</a></li><li><a href="/front-end-playground/front-end-others/d3-tree/5-add-panel.html" class="sidebar-link">5. 添加浮层</a></li><li><a href="/front-end-playground/front-end-others/d3-tree/6-add-smart-menu.html" class="sidebar-link">6. 添加右键菜单</a></li><li><a href="/front-end-playground/front-end-others/d3-tree/7-text-path.html" class="sidebar-link">7. 曲线上的文字 textPath</a></li><li><a href="/front-end-playground/front-end-others/d3-tree/8-add-path-click.html" class="sidebar-link">8. 曲线 hover 和点击</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0" style="padding-top:;"><!----> <p class="sidebar-heading"><span>试试 Three.js</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0" style="padding-top:;"><!----> <p class="sidebar-heading"><span>BOX2D.js 文档与教程</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><p>因为业务需要折腾起了图表，该系列用于记录使用该库制作 Tree 图表的一些笔记。本文介绍动态获取节点数据，并添加子节点的过程。</p> <h1 id="动态获取子节点数据"><a href="#动态获取子节点数据" class="header-anchor">#</a> 动态获取子节点数据</h1> <h2 id="拆分模块"><a href="#拆分模块" class="header-anchor">#</a> 拆分模块</h2> <p>在后面越来越多的功能增加之前，我们要把代码进行下整理，模块拆分首先就是一个整理逻辑的过程。</p> <p>首先，我们的 Demo 大概可以拆分成这些模块：</p> <ol><li>初始化 D3 基本能力（设置宽高等）。</li> <li>数据初始化绑定。</li> <li>数据更新绑定。</li> <li>node 交互和绘制。</li> <li>link 交互和绘制。</li> <li>单击节点事件处理。</li> <li>双击获取子节点事件处理（本节新增）。</li></ol> <p>由于代码篇幅稍微多一些，这里调整后一致在文末体现哈。</p> <h2 id="tree-node"><a href="#tree-node" class="header-anchor">#</a> Tree node</h2> <p><strong>d3.tree(root)</strong>
使用默认的设置构建一个树图布局生成器。</p> <p>根据指定的根节点代表的 hierarchy 数据生成一个树状布局数据，每个节点包含以下属性:</p> <ul><li>node.x：节点的 x 坐标</li> <li>node.y：节点的 y 坐标</li></ul> <p>为了动态获取时，能有效生成子节点，我们来看看一个 tree node 里面包含了哪些属性：</p> <p><img src="https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/1513514218%281%29.png" alt="image"></p> <p>由于<code>d3.tree()</code>能自动计算生成 x 和 y 位置，而 id、x0、y0 等则是批量处理生成，故我们主要需要添加的属性有：</p> <ul><li>depth：该节点的层级数，主要用于 y 的计算，避免重叠</li> <li>parent：保存父节点的信息，用于绘制 link 关系</li> <li>children：保存子节点的信息</li></ul> <p>这里分析 tree node 的信息，用于后面动态获取节点信息，手动添加子节点和更新图表。</p> <h2 id="绑定双击事件"><a href="#绑定双击事件" class="header-anchor">#</a> 绑定双击事件</h2> <p>因为我们上面使用了单击来展开和收起子节点，因此这里我们将使用双击来获取子节点。我们在节点 enter 时增加的 click 事件，这里我们再添加个 dbclick 的事件绑定：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 添加enter操作，添加类名为node的group元素</span>
<span class="token keyword">var</span> nodeEnter <span class="token operator">=</span> node
  <span class="token punctuation">.</span><span class="token function">enter</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  <span class="token comment">// ...其他操作</span>
  <span class="token comment">// 给每个新加的节点绑定click事件</span>
  <span class="token punctuation">.</span><span class="token function">on</span><span class="token punctuation">(</span><span class="token string">&quot;click&quot;</span><span class="token punctuation">,</span> click<span class="token punctuation">)</span>
  <span class="token comment">// 给每个新加的节点绑定dbclick事件</span>
  <span class="token punctuation">.</span><span class="token function">on</span><span class="token punctuation">(</span><span class="token string">&quot;dblclick&quot;</span><span class="token punctuation">,</span> dblclick<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>然后我们需要添加 dblclick 函数，主要处理逻辑包括：</p> <ol><li>检查该节点是否已经请求过，已请求过则忽略。</li> <li>随机请求<code>1.json</code> - <code>5.json</code>随机获取数据。</li> <li>获取到数据后，手动给节点添加子节点，同时子节点设置 parent 为该节点。</li> <li>更新图表。</li></ol> <p>我们将请求到的数据存在节点的<code>data</code>属性中，同时根据上面 tree node 节点的分析，我们需要手动给子节点添加对应的属性，包括<code>name</code>、<code>depth</code>、<code>parent</code>等。</p> <p>我们能得到这样的 dblclick 函数：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 将获取到的节点，添加进data对象中，同时若已获取过不再获取</span>
<span class="token keyword">function</span> <span class="token function">dblclick</span><span class="token punctuation">(</span><span class="token parameter">d</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment">// 若无d.data.children，则视为未获取</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token punctuation">(</span>d<span class="token punctuation">.</span>data <span class="token operator">&amp;&amp;</span> d<span class="token punctuation">.</span>data<span class="token punctuation">.</span>children<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// 这里模拟请求，1.json - 5.json 随机获取数据</span>
    <span class="token keyword">var</span> randomNum <span class="token operator">=</span> Math<span class="token punctuation">.</span><span class="token function">floor</span><span class="token punctuation">(</span>Math<span class="token punctuation">.</span><span class="token function">random</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token number">5</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span>
    d3<span class="token punctuation">.</span><span class="token function">json</span><span class="token punctuation">(</span>randomNum <span class="token operator">+</span> <span class="token string">&quot;.json&quot;</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">error<span class="token punctuation">,</span> data</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">if</span> <span class="token punctuation">(</span>error<span class="token punctuation">)</span> <span class="token keyword">throw</span> error<span class="token punctuation">;</span>
      <span class="token comment">// 给子节点绑定父节点</span>
      <span class="token keyword">var</span> children <span class="token operator">=</span> data<span class="token punctuation">.</span>children<span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span><span class="token parameter">x</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token punctuation">{</span>
          name<span class="token operator">:</span> x<span class="token punctuation">.</span>name<span class="token punctuation">,</span>
          parent<span class="token operator">:</span> d<span class="token punctuation">,</span>
          depth<span class="token operator">:</span> d<span class="token punctuation">.</span>depth <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span>
          data<span class="token operator">:</span> <span class="token punctuation">{</span>
            <span class="token operator">...</span>x
          <span class="token punctuation">}</span>
        <span class="token punctuation">}</span><span class="token punctuation">;</span>
      <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token comment">// 将子节点数据绑定在d节点上</span>
      <span class="token comment">// 若子节点为空，则不执行</span>
      <span class="token keyword">if</span> <span class="token punctuation">(</span>children<span class="token punctuation">.</span>length<span class="token punctuation">)</span> d<span class="token punctuation">.</span>children <span class="token operator">=</span> children<span class="token punctuation">;</span>
      <span class="token comment">// 同时也绑到data上</span>
      d<span class="token punctuation">.</span>data<span class="token punctuation">.</span>children <span class="token operator">=</span> children<span class="token punctuation">;</span>
      <span class="token function">updateChart</span><span class="token punctuation">(</span>d<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>上面为什么我们要使用<code>data.children</code>来判断是否已经加载过呢？这是因为 d3 中 node 的 children 不接受空数组，只能为非空数组或是<code>null</code>。而<code>null</code>可能有两种情况：收起、无子节点。同时为了方便后续数据处理，我们使用<code>data</code>属性来保存一切获取来的信息数据。</p> <p>准备就绪，我们将初始的数据设置为首层节点数据：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> treeData <span class="token operator">=</span> <span class="token punctuation">{</span>
  name<span class="token operator">:</span> <span class="token string">&quot;Top Level&quot;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></div><h2 id="节点状态定义"><a href="#节点状态定义" class="header-anchor">#</a> 节点状态定义</h2> <p>因为加入 Ajax 获取信息后，节点的状态将会增加，我们给节点添加一些规则：</p> <ol><li>当未获取子节点信息时，将节点填充灰色，来表示待获取。</li> <li>当节点拥有子节点、并处于收起状态时，将节点填充浅蓝色，表示拥有子节点。</li> <li>当节点查无子节点，或是已展开，均无填充状态。</li></ol> <p>其实只增加了一种状态：是否已获取数据。我们会需要在节点 enter 以及 update 的时候需要增加这个状态。同上，我们使用<code>data.children</code>来判断是否已经加载过：</p> <div class="language-js extra-class"><pre class="language-js"><code>node
  <span class="token comment">// ...其他操作</span>
  <span class="token punctuation">.</span><span class="token function">style</span><span class="token punctuation">(</span><span class="token string">&quot;fill&quot;</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">d</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> d<span class="token punctuation">.</span>_children <span class="token operator">?</span> <span class="token string">&quot;lightsteelblue&quot;</span> <span class="token operator">:</span> d<span class="token punctuation">.</span>data<span class="token punctuation">.</span>children <span class="token operator">?</span> <span class="token string">&quot;#fff&quot;</span> <span class="token operator">:</span> <span class="token string">&quot;#ccc&quot;</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h2 id="双击单击的冲突解决"><a href="#双击单击的冲突解决" class="header-anchor">#</a> 双击单击的冲突解决</h2> <p>我们在使用中会发现，双击事件同时会触发两次的单击时间，这其实是不合理的，我们通过添加定时器来解决：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 当点击时，切换children，同时用_children来保存原子节点信息</span>
<span class="token keyword">function</span> <span class="token function">click</span><span class="token punctuation">(</span><span class="token parameter">d</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>d<span class="token punctuation">.</span>_clickid<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// 若在200ms里面点击第二次，则不做任何操作，清空定时器</span>
    <span class="token function">clearTimeout</span><span class="token punctuation">(</span>d<span class="token punctuation">.</span>_clickid<span class="token punctuation">)</span><span class="token punctuation">;</span>
    d<span class="token punctuation">.</span>_clickid <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
    <span class="token comment">// 首次点击，添加定时器，200ms后进行toggle</span>
    d<span class="token punctuation">.</span>_clickid <span class="token operator">=</span> <span class="token function">setTimeout</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
      <span class="token keyword">if</span> <span class="token punctuation">(</span>d<span class="token punctuation">.</span>children<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        d<span class="token punctuation">.</span>_children <span class="token operator">=</span> d<span class="token punctuation">.</span>children<span class="token punctuation">;</span>
        d<span class="token punctuation">.</span>children <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
      <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
        d<span class="token punctuation">.</span>children <span class="token operator">=</span> d<span class="token punctuation">.</span>_children<span class="token punctuation">;</span>
        d<span class="token punctuation">.</span>_children <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
      <span class="token punctuation">}</span>
      <span class="token function">updateChart</span><span class="token punctuation">(</span>d<span class="token punctuation">)</span><span class="token punctuation">;</span>
      d<span class="token punctuation">.</span>_clickid <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>最终我们效果将会如图：
<img src="https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/1513505937%281%29.png" alt="image"></p> <h1 id="结束语"><a href="#结束语" class="header-anchor">#</a> 结束语</h1> <p>本节我们添加了节点动态获取子节点的功能，同时给节点加上已读状态，同时处理好单击双击的事件冲突。<br>
我们在完成基本需求的同时，也需要主动改善用户体验，或是不合理的地方要主动提出来，因为说好的要把最棒的一面呈现世间的呢。<br> <a href="https://github.com/godbasin/godbasin.github.io/tree/blog-codes/d3-tree-notes/3-ajax-for-node" target="_blank" rel="noopener noreferrer">此处查看项目代码<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a><br> <a href="http://d3.godbasin.com/3-ajax-for-node/index.html" target="_blank" rel="noopener noreferrer">此处查看页面效果<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p></div> <!----> <footer class="page-edit"><div class="edit-link"><a href="https://github.com/godbasin/front-end-playground/edit/sourcecode/docs/front-end-others/d3-tree/3-ajax-for-node.md" target="_blank" rel="noopener noreferrer">帮阿猪改善此页面！</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <!----> <blockquote>部分文章中使用了一些网站的截图，如果涉及侵权，请告诉我删一下谢谢~</blockquote> <div style="margin-top:30px;"><div class="el-row" style="margin-left:-10px;margin-right:-10px;"><div class="el-col el-col-24 el-col-sm-0 el-col-md-2 el-col-lg-4" style="padding-left:10px;padding-right:10px;display:block;"><div style="width:1px;height:1px;"></div></div> <div class="el-col el-col-24 el-col-sm-24 el-col-md-18 el-col-lg-16" style="padding-left:10px;padding-right:10px;"><div class="el-card box-card is-always-shadow"><div class="el-card__header"><div class="clearfix"><span>温馨提示喵</span></div></div><div class="el-card__body"> <div class="el-row" style="margin-left:-10px;margin-right:-10px;"><div class="el-col el-col-24 el-col-xs-24 el-col-sm-12" style="padding-left:10px;padding-right:10px;"><div class="el-image"><div class="image-slot"><img src="https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/assets/img/loading.gif" style="width:100%;"></div><!----></div></div> <div class="el-col el-col-24 el-col-xs-24 el-col-sm-12" style="padding-left:10px;padding-right:10px;"><div class="copyright-text"><div>本文版权归作者所有，欢迎转载，但未经作者同意必须保留此段声明，且在文章页面明显位置给出原文连接，否则保留追究法律责任的权利。</div> <div>出处：被删的前端游乐场</div> <div>作者：<a href="https://github.com/godbasin" target="_blank">被删</a></div></div></div></div></div></div></div></div></div></footer> <div class="page-nav"><p class="inner"><span class="prev">
        ←
        <a href="/front-end-playground/front-end-others/d3-tree/2-init-a-d3-tree.html" class="prev">
          2. 简单的 Tree demo
        </a></span> <span class="next"><a href="/front-end-playground/front-end-others/d3-tree/4-zoom-amd-drag.html">
          4. 添加拖动和缩放
        </a>
        →
      </span></p></div>  <div class="gitalk-container theme-default-content"><div id="gitalk-container" class="content"></div></div></main> <div id="kitty-container"><span><div role="tooltip" id="el-popover-2282" aria-hidden="true" class="el-popover el-popper" style="width:undefinedpx;display:none;"><!----><img src="https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/2code2.jpg" class="image"> <div class="text">牡羊猪的猫粮罐</div> </div><span class="el-popover__reference-wrapper"><div id="kitty" style="background:url(https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/assets/img/kitty0.svg);"></div></span></span> <div class="el-dialog__wrapper" style="display:none;"><div role="dialog" aria-modal="true" aria-label="牡羊猪是这样渐渐胖成猪的喵（点击图片可以切换噢）" class="el-dialog" style="margin-top:15vh;"><div class="el-dialog__header"><span class="el-dialog__title">牡羊猪是这样渐渐胖成猪的喵（点击图片可以切换噢）</span><button type="button" aria-label="Close" class="el-dialog__headerbtn"><i class="el-dialog__close el-icon el-icon-close"></i></button></div><!----><!----></div></div></div></div><div class="global-ui"></div></div>
    <script src="/front-end-playground/assets/js/app.1e2670bf.js" defer></script><script src="/front-end-playground/assets/js/2.38d016d1.js" defer></script><script src="/front-end-playground/assets/js/3.e3f029cb.js" defer></script><script src="/front-end-playground/assets/js/97.2386ebe1.js" defer></script>
  </body>
</html>
